﻿//////////////////////////////////////////////////////////////////////////////
//
// Copyright © 1998-2024 Glodon Company Limited.
//
// Licensed under the MIT License
//
// Permission is hereby granted, free of charge, to any person obtaining a
// copy of this software and associated documentation files (the “Software”),
// to deal in the Software without restriction, including without limitation
// the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or
// sell copies of the Software, and to permit persons to whom the Software is
// furnished to do so, subject to the following conditions:
//
// The above copyright notice and this permission notice shall be included in
// all copies or substantial portions of the Software.
//
// THE SOFTWARE IS PROVIDED “AS IS”, WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
// IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
// FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL
// THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
// LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
// OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
//
//////////////////////////////////////////////////////////////////////////////
#include "MenuItemContainerDefinition.h"
#include "GbmpNew.h"
#include "ICommandMenuItemDefinition.h"
#include "ICommand.h"
#include "ICommandManager.h"
#include "MenuItemDefinition.h"
#include "EnableCompileWarning_The_LAST_IncludeInCpp.h"

using namespace gcmp;

gcmp::MenuItemContainerDefinition::~MenuItemContainerDefinition()
{

}

gcmp::MenuItemContainerDefinition::MenuItemContainerDefinition(const std::wstring& id, const std::wstring& caption)
    :m_menuItemDefine(NEW_AS_OWNER_PTR(MenuItemDefinition, id, L"", caption, L"", L""))
{

}

std::wstring gcmp::MenuItemContainerDefinition::GetId() const
{
    return m_menuItemDefine->GetId();
}

std::wstring gcmp::MenuItemContainerDefinition::GetCaption() const
{
    return m_menuItemDefine->GetCaption();
}

std::wstring gcmp::MenuItemContainerDefinition::GetTooltip() const
{
    return m_menuItemDefine->GetTooltip();
}

std::wstring gcmp::MenuItemContainerDefinition::GetCommandId() const
{
    return m_menuItemDefine->GetCommandId();
}

std::wstring gcmp::MenuItemContainerDefinition::GetIconPath() const
{
    return m_menuItemDefine->GetIconPath();
}

std::vector<const IMenuItemDefinition* > gcmp::MenuItemContainerDefinition::GetSubMenuItems() const
{
    return m_menuItemDefine->GetSubMenuItems();
}

bool gcmp::MenuItemContainerDefinition::AddSubMenuItem(OwnerPtr<IMenuItemDefinition> pSubMenuItem)
{
    return m_menuItemDefine->AddSubMenuItem(TransferOwnership(pSubMenuItem));
}

bool gcmp::MenuItemContainerDefinition::AddSeparator()
{
    OwnerPtr<IMenuItemDefinition> pSeparatorItem = NEW_AS_OWNER_PTR(SeparatorItemDefinition);
    return m_menuItemDefine->AddSubMenuItem(TransferOwnership(pSeparatorItem));
}


bool gcmp::MenuItemContainerDefinition::IsVisible() const
{
    // MenuItemContainerDefinition包含的iten都不可见，那么其也不可见。
    std::vector<const IMenuItemDefinition*> menuItems = GetSubMenuItems();
    for (auto pMenuItem : menuItems)
    {
        DBG_WARN_AND_CONTINUE_UNLESS(pMenuItem, L"pControlDefinition为空",L"GDMPLab",L"2024-03-30");
        bool visible = false;
        IsMenuItemVisible(pMenuItem, visible);
        if (visible)
        {
            return true;
        }
    }
    return false;
}

void gcmp::MenuItemContainerDefinition::IsMenuItemVisible(const gcmp::IMenuItemDefinition* pMenuItemDefinition, bool& visible) const
{
    DBG_WARN_AND_RETURN_VOID_UNLESS(pMenuItemDefinition, L"pMenuItemDefinition为空",L"GDMPLab",L"2024-03-30");
    if (const ICommandMenuItemDefinition* define = dynamic_cast<const ICommandMenuItemDefinition*>(pMenuItemDefinition))
    {
        ICommand* pMenuItemCommand = ICommandManager::Get()->GetCommand(define->GetCommandId());
        if (!pMenuItemCommand)
        {
            visible = true;
            return;
        }
        if (pMenuItemCommand->IsVisible())
        {
            visible = true;
            return;
        }
    }
    std::vector<const IMenuItemDefinition* > subMenuItems = pMenuItemDefinition->GetSubMenuItems();
    if (subMenuItems.empty())
        return;

    for (auto iter : subMenuItems)
    {
        IsMenuItemVisible(iter, visible);
        if (visible)
        {
            return;
        }
    }
}
